//
//  HNSH_ChatFriendHeader.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/2/22.
//

#import "HNSH_ChatFriendHeader.h"

@interface HNSH_ChatFriendItem : UIView

/* iconImgV */
@property (nonatomic, strong) UIImageView *iconImgV;
/* titleLabel */
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong)UILabel * countLabel;
/* clickBlock */
@property (nonatomic, copy) dispatch_block_t clickBlock;

@end

@implementation HNSH_ChatFriendItem

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self configUI];
    }
    return self;
}

- (void)configUI {
    self.backgroundColor = [UIColor appWhiteLabelColor];
    [self addSubview:self.iconImgV];
    [self addSubview:self.titleLabel];
    [self addSubview:self.countLabel];
    
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(78, 49, kScreenWidth-78, 1)];
    lineView.backgroundColor = [UIColor colorWithHexString:@"#EFEFF4"];
    [self addSubview:lineView];
    
    self.userInteractionEnabled = YES;
    WeakSelf(weakSelf);
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithActionBlock:^(id  _Nonnull sender) {
        if (weakSelf.clickBlock) {
            weakSelf.clickBlock();
        }
    }];
    [self addGestureRecognizer:tap];
}

#pragma mark lazy
- (UIImageView *)iconImgV {
    if (!_iconImgV) {
        _iconImgV = [[UIImageView alloc]initWithFrame:CGRectMake(16, 5, 40, 40)];
    }
    return _iconImgV;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(68, 5, 200, 40)];
        _titleLabel.textColor = [UIColor appBlackLabelColor];
        _titleLabel.font = [UIFont systemFontOfSize:14];
    }
    return _titleLabel;
}

- (UILabel *)countLabel{
    if (!_countLabel) {
        _countLabel = [[UILabel alloc]initWithFrame:CGRectMake(kScreenWidth-36, 15, 20, 20)];
        _countLabel.textAlignment = NSTextAlignmentCenter;
        _countLabel.adjustsFontSizeToFitWidth = YES;
        _countLabel.textColor = [UIColor whiteColor];
        _countLabel.font = [UIFont systemFontOfSize:8];
        _countLabel.layer.cornerRadius = 10;
        _countLabel.layer.masksToBounds = YES;
        _countLabel.backgroundColor = [UIColor appGreenColor];
        _countLabel.hidden = YES;
    }
    return _countLabel;
}

@end

@interface HNSH_ChatFriendHeader ()

/* newFriendItem */
@property (nonatomic, strong) HNSH_ChatFriendItem *friendItem;

@end

@implementation HNSH_ChatFriendHeader

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self configUI];
    }
    return self;
}

- (void)setUnreadCount:(NSInteger)unreadCount {
    _unreadCount = unreadCount;
    self.friendItem.countLabel.hidden = unreadCount <= 0;
    self.friendItem.countLabel.text = [NSString stringWithFormat:@"%ld",unreadCount];
}

- (void)configUI {
    //新的朋友
    HNSH_ChatFriendItem *newFriendItem = [[HNSH_ChatFriendItem alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 50)];
    newFriendItem.iconImgV.image = [UIImage imageNamed:@"chat_home_txl_header_friend"];
    newFriendItem.titleLabel.text = @"新的朋友";
    [self addSubview:newFriendItem];
    self.friendItem = newFriendItem;
    
    //我的群聊
    HNSH_ChatFriendItem *myGroupItem = [[HNSH_ChatFriendItem alloc]initWithFrame:CGRectMake(0, 50, kScreenWidth, 50)];
    myGroupItem.iconImgV.image = [UIImage imageNamed:@"chat_home_txl_header_group"];
    myGroupItem.titleLabel.text = @"我的群聊";
    [self addSubview:myGroupItem];
    
    WeakSelf(weakSelf);
    newFriendItem.clickBlock = ^{
        if (weakSelf.indexBlock) {
            weakSelf.indexBlock(0);
        }
    };
    myGroupItem.clickBlock = ^{
        if (weakSelf.indexBlock) {
            weakSelf.indexBlock(1);
        }
    };
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
